package com.clwl.supervise.superviseapp.controller.inspection;


import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clwl.supervise.superviseapp.config.RepeatSubmit;
import com.clwl.supervise.superviseapp.entity.base.CommonSearchEntity;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.supervise.JgSchemeContent;
import com.clwl.supervise.superviseapp.entity.supervise.JgSchemePlanCheck;
import com.clwl.supervise.superviseapp.service.inspect.SchemePlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 体系检查
 */
@Slf4j
@RestController
@RequestMapping("schemePlan")
public class SchemePlanController {


    /**
     * 体系检查列表信息
     * @param pageNum
     * @param pageSize
     * @param params 请求参数
     * @return
     */
    @GetMapping("getList")
    private ResultEntity<IPage<Map<String,Object>>> getCompanyList(@RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize, CommonSearchEntity params){
        SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
        IPage<Map<String,Object>> pageList = schemePlanService.selectCompanyPage(pageNum,pageSize,params);
        return ResultEntity.success(pageList);
    }

    /**
     * 体系检查列表信息
     * @param checkId  检查单id
     * @return
     */
    @GetMapping("schemeCheckInfo")
    private ResultEntity<JgSchemePlanCheck> schemeCheckInfo(Long checkId){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            JgSchemePlanCheck checkInfo = schemePlanService.getSchemeCheckInfo(checkId);
            return ResultEntity.success(checkInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取信息失败");
        }
    }

    /**
     * 保存检查门头照照片
     * @param checkId 检查Id
     * @param file 门头照文件
     * @return
     */
    @PostMapping("uploadCheckFile")
    public ResultEntity uploadCheckFile(Long checkId, MultipartFile file){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            JgSchemePlanCheck check =  schemePlanService.uploadCheckFile( checkId, file);
            return ResultEntity.success(check);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 保存告知单
     * @param checkId
     * @param informFile
     * @param taskIds 关联的日常检查任务的任务id
     * @return
     */
    @PostMapping("saveCheckInform")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity saveCheckInform(Long checkId, String  informFile ,String taskIds){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            String  filePath = schemePlanService.saveCheckInform(checkId,   informFile,taskIds);
            return ResultEntity.success(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }



    /**
     * 保存/临时保存检查项结果内容
     * @param temporary 检查id
     * @param checkData 联系电话
     * @param taskIds 关联的日常检查任务的任务id
     * @return
     */
    @PostMapping("saveCheckInfo")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity saveCheckInfo(String temporary,  String checkData ,String taskIds){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            JgSchemePlanCheck schemePlanCheck = JSONObject.parseObject(checkData, JgSchemePlanCheck.class);
            JgSchemePlanCheck dailyCheck =  schemePlanService.saveCheckInfo(temporary,schemePlanCheck,taskIds);
            return ResultEntity.success(dailyCheck);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存/临时保存检查项结果内容失败");
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 最终保存
     * @param checkId
     * @param taskIds 关联的日常检查任务的任务id
     * @return
     */
    @PostMapping("finaSaveInfo")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity finaSaveInfo(Long checkId,String taskIds){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            JgSchemePlanCheck dailyCheck =  schemePlanService.finaSaveInfo(checkId,taskIds);
            return ResultEntity.success(dailyCheck);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存/临时保存检查项结果内容失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 上传结果记录的文件
     * @param checkId 日常检查结果表id
     * @param imgFile 二进制文件流
     * @param type 1-要点表；2-记录表
     * @return
     */
    @PostMapping("uploadResultRecord")
    public ResultEntity uploadResultRecord(Long checkId,@RequestPart(required = true,name = "imgFile") MultipartFile imgFile,String type
    ){
        try {
            if (imgFile != null && StringUtils.isBlank(imgFile.getOriginalFilename())) {
                return ResultEntity.error("请上传文件");
            }
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            String fileUrl = schemePlanService.uploadResultRecord(checkId,imgFile,type);
            return ResultEntity.success(fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("上传结果记录的文件失败");
            return ResultEntity.error("操作失败");
        }
    }





    /**
     * 查询检查内容的列表
     * @param checkId 日常检查表JG_DAILY_CHECK  id
     * @return
     */
    @GetMapping("getCheckInfo")
    public ResultEntity getCheckInfo(Long checkId){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            JgSchemePlanCheck checkInfo = schemePlanService.getCheckInfo(checkId);
            return ResultEntity.success(checkInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查信息失败");
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 查询结果表需要的数据
     * @param checkId
     * @return
     */
    @GetMapping("getResultTable")
    public ResultEntity getResultTable(Long checkId){
        try {
            SchemePlanService dailyInspectService = SpringUtil.getBean(SchemePlanService.class);
            Map<String,Object> resultTable = dailyInspectService.getResultTable(checkId);
            return ResultEntity.success(resultTable);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查信息失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 查询检查完成的订单
     * @param checkId
     * @return
     */
    @GetMapping("getCheckResultInfo")
    public ResultEntity getCheckResultInfo(Long checkId){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            Map<String, Object> result = schemePlanService.getCheckResultInfo(checkId);
            return ResultEntity.success(result);
        } catch (Exception e) {
            log.error("查询检查完成的订单失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 查询检查内容的列表
     * @param standardId
     * @return
     */
    @GetMapping("getCheckContentList")
    public ResultEntity getCheckContentList(Long standardId){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            List<JgSchemeContent> contentList = schemePlanService.getCheckContentList(standardId);
            return ResultEntity.success(contentList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查内容的列表失败");
            return ResultEntity.error("操作失败");
        }
    }


    /**
     * 获取可关联合并的日常检查任务
     * @param companyId 企业id
     * @return
     */
    @GetMapping("getDailyMergeInspectList")
    public ResultEntity getDailyMergeInspectList(Long companyId){
        try {
            SchemePlanService schemePlanService = SpringUtil.getBean(SchemePlanService.class);
            List<Map<String,Object>> contentList = schemePlanService.getDailyMergeInspectList(companyId);
            return ResultEntity.success(contentList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询检查内容的列表失败");
            return ResultEntity.error("操作失败");
        }
    }


}
